﻿@model Controller2.Models.ViewModels.FornecedorViewModel
@{
    Layout = null;
}
@if (!Request.IsAjaxRequest())
{
    <link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />
    <link href="../../Content/themes/controller/controller.css" rel="stylesheet" type="text/css" />
    @(Html.Telerik().StyleSheetRegistrar().DefaultGroup(group => group.Add("telerik.common.css").Add("telerik.vista.css").Combined(true).Compress(true)))  
    <script src="@Url.Content("~/Scripts/jquery.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery-ui.js")" type="text/javascript"></script>    
    <script src="@Url.Content("~/Scripts/jquery.maskedinput-1.3.min.js")" type="text/javascript"></script>    
    <script src="@Url.Content("~/Scripts/modernizr-1.7.min.js")" type="text/javascript"></script>
}
@if (false)
{
    <script src="../../Scripts/jquery.js" type="text/javascript"></script>
    <script src="../../Scripts/controller.js" type="text/javascript"></script>
    <script src="../../Scripts/jquery-vsdoc.js" type="text/javascript"></script>
    <script src="../../Scripts/jquery.validate-vsdoc.js" type="text/javascript"></script>
    <script src="../../Scripts/jquery-ui.js" type="text/javascript"></script>
}
<script src="@Url.Content("~/Scripts/controller.js")" type="text/javascript"></script>
<div id="corpoPesquisa">
    @MvcHtmlString.Create(@ViewBag.Pesquisa)
    @(Html.Telerik().Grid<Controller2.Models.ViewModels.FornecedorViewModel>()
    .Name("grdConsultaFornecedor")
    .Selectable()
    .ClientEvents(events => events.OnRowSelected("PesquisaOnRowSelected")
                                      .OnLoad("PesquisaGrid_onLoad"))
    .Columns(c =>
    {
        c.Bound(g => g.ID).Title("Código").Width(60).HtmlAttributes(new { style = "text-align: center;" });
        c.Bound(g => g.Nome).Title("Nome");
    })
    .TableHtmlAttributes(new { style = "font-size: 8pt;" })
    .DataBinding(d => d
        .Ajax().Select("AjaxPesquisaFornecedor", "Fornecedor")
    )
    .PrefixUrlParameters(false)
    .Pageable(p => p.Enabled(true)
        .PageSize(20)
            .Position(GridPagerPosition.Bottom)
        .Style(GridPagerStyles.NextPreviousAndNumeric | GridPagerStyles.PageInput)
    )
    .Sortable()
    .Scrollable(s => s.Height(200))
    .Filterable(f => f.Enabled(false))
    .Groupable(g => g.Enabled(false))
)
    <div id="botoesForm">
        <input type="button" id="Selecionar" value="Selecionar" />
        <input type="button" id="FecharPesquisa" value="Fechar" />
    </div>
    <div id="alerta" style="display: none;">
        Por favor, selecione um regristro!
    </div>
</div>
<script type="text/javascript">
    $(function () {
        $('#txtPesquisaFornecedor').autocomplete({
            source: function (request, response) {
                $.post('@Url.Action("AutoCompletePesquisaFornecedor", "Fornecedor")', { termoPesquisa: request.term }, function (data) {
                    return response(data);
                }, 'json');
            },
            delay: 100, minLength: 1
        });
        hideLoading();
    });
    $('#btnPesquisaFornecedor').click(function (e) {
        showLoading(); e.preventDefault(); pesquisa();
    });
    function pesquisa() {
        var texto = $('#txtPesquisaFornecedor').val();
        var url = '@Url.Action("TermosPesquisa", "Fornecedor")';
        $.post(url, { texto: texto }, function (result) {
            gridRebind('grdConsultaFornecedor');
            hideLoading();
        });
    }
    $('#Selecionar').click(function (e) {
        showLoading();
        if (id === undefined || id === '') {
            alertDialog('alerta', '', 120, 250);
            hideLoading();
            return false;
        } else {
            preencherCamposPesquisaFornecedor(id);
            closeWindowPesquisa();
        }
    });
    $('#FecharPesquisa').live('click', function () {
        closeWindowPesquisa();
    });
</script>
